//---------------------------------------------------------------------------
// Copyright (c) 2010 Texas Instruments, Inc.
// This is an unpublished work created in the year stated above.
// Texas Instruments owns all rights in and to this work and
// intends to maintain and protect it as an unpublished copyright.
// In the event of either inadvertent or deliberate publication,
// the above stated date shall be treated as the year of first
// publication. In the evxfent of such publication, Texas Instruments
// intends to enforce its rights in the work under the copyright
// laws as a published work.
//----------------------------------------------------------------------------
// Filename                : message_protocol_application.c
//
// Functional Description  : This file contains the overridden functions for the common
//                           messaging protocol. This allows the common messaging component
// 							 to work while allowing application extensions to the protocol
//							 in a common way. 
//
//-----------------------------------------------------------------------------


#include "DRV922XX.h"
#include "message_protocol_application.h"
#include "afe_data.h"

// Data for common messaging protocol

unsigned int MsgDeviceId = DEVICE_ID;
unsigned int MsgFwVersion = FW_VERSION;

// Application Data (RAM)

unsigned int AppAdminState;																/*!< Administrative state (bits defined in AppAdminStateBits */
unsigned int AppHwOc;																	/*!< Hardware OC threshold */
unsigned int AppDeadTime;																/*!< Dead-time for complementary PWM mode, in 25MHz CCR counts */
unsigned int AppIfilterMax;																/*!< Maximum current threshold before system starts duty cycle limiting */
unsigned int AppIfilterMid;																/*!< Maximum current threhold before system starts holding duty cycle */
unsigned int AppImaxNonCmpl;															/*!< Current threshold for switching from non-complementary to complementary PWM */
unsigned int AppImaxNonCmplHys;															/*!< Hysteresis current used for switching back from complementary to non-complementary PWM */
unsigned int AppThrottleMinVoltage;														/*!< Minimum throttle voltage that must be reached before controller attempts to drive PWM */
unsigned int AppPwmLowUpperBound;														/*!< Upper bound of low PWM duty cycle range (for determining measurement points of voltage, current, BEMF */
unsigned int AppPwmMidUpperBound;														/*!< Upper bound of mid PWM duty cycle range (for determining measurement points of voltage, current, BEMF */
unsigned int AppPwmMinDuty;																/*!< Minimum PWM duty cycle */
unsigned int AppOffThrottle;

unsigned int AppStatus = 0;																/*!< 16b status word defined by StatusBits enumeration */
unsigned int AppSpeed = 0xFFFF;															/*!< 1ms timer ticks for last sampled 60-degree rotation of motor */
unsigned int AppThrottleVoltage = 0;													/*!< last sampled throttle voltage */
unsigned int AppThrottleVoltageFiltered = 0;											/*!< last calculated filtered throttle voltage */
unsigned int AppThrottleVoltage_Reference = 0; 
unsigned int AppTemperature = 0;
unsigned int AppCurrent = 0;
unsigned int AppCurrentFiltered = 0;
unsigned int AppDutyRange = 0;//PwmDutyRangeLow;											/*!< PWM duty cycle range (used for sample measurement points) */
unsigned int AppPwmDuty = 0;															/*!< PWM duty cycle in 25MHz ticks */
unsigned int AppMotorState = 0;																/*!< Motor State */
unsigned int AppFanOff;// = 1;
unsigned int AppOverCurrent = 0;

// Application Data (FRAM)

const unsigned int AppOffThrottleSaved = DEFAULT_OFF_THROTTLE;
const unsigned int AppAdminStateSaved = DEFAULT_ADMIN_STATE;							/*!< Administrative state (bits defined in AppAdminStateBits */
const unsigned int AppHwOcSaved = DEFAULT_HW_OC;										/*!< Hardware over-current threshold */
const unsigned int AppDeadTimeSaved = DEFAULT_DEAD_TIME;								/*!< Dead-time for complementary PWM mode, in 25MHz CCR counts */
const unsigned int AppIfilterMaxSaved = DEFAULT_IFILT_MAX;								/*!< Maximum current threshold before system starts duty cycle limiting */
const unsigned int AppIfilterMidSaved = DEFAULT_IFILT_MID;								/*!< Maximum current threhold before system starts holding duty cycle */
const unsigned int AppImaxNonCmplSaved = DEFAULT_I_MAX_NONCMPL_PWM;						/*!< Current threshold for switching from non-complementary to complementary PWM */
const unsigned int AppImaxNonCmplHysSaved = DEFAULT_I_NONCMPL_PWM_HYS;					/*!< Hysteresis current used for switching back from complementary to non-complementary PWM */
const unsigned int AppThrottleMinVoltageSaved = DEFAULT_THROTTLE_MIN_V;					/*!< Minimum throttle voltage that must be reached before controller attempts to drive PWM */
const unsigned int AppPwmLowUpperBoundSaved = DEFAULT_PWM_LOW_UPPER_BOUND;				/*!< Upper bound of low PWM duty cycle range (for determining measurement points of voltage, current, BEMF */
const unsigned int AppPwmMidUpperBoundSaved = DEFAULT_PWM_MID_UPPER_BOUND;				/*!< Upper bound of mid PWM duty cycle range (for determining measurement points of voltage, current, BEMF */
const unsigned int AppPwmMinDutySaved = DEFAULT_PWM_MIN_DUTY;							/*!< Minimum PWM duty cycle */




/*
 *  \fn void msg_protocol_init_app()
 *  \brief Initializes the application parameters by reading them from persistent memory and storing in RAM
 */
 
void message_protocol_init_app()
{
	AppAdminState = AppAdminStateSaved;
	AppHwOc = AppHwOcSaved;
	AppDeadTime = AppDeadTimeSaved;
	AppIfilterMax = AppIfilterMaxSaved;
	AppIfilterMid = AppIfilterMidSaved;
	AppImaxNonCmpl = AppImaxNonCmplSaved;
	AppImaxNonCmplHys = AppImaxNonCmplHysSaved;
	AppThrottleMinVoltage = AppThrottleMinVoltageSaved;
	AppPwmLowUpperBound = AppPwmLowUpperBoundSaved;
	AppPwmMidUpperBound = AppPwmMidUpperBoundSaved;
	AppPwmMinDuty = AppPwmMinDutySaved;
	AppOffThrottle = AppOffThrottleSaved;
}
/*
 *  \fn void message_handler_application_save_parameters()
 *  \brief Callback function from common message handler for saving application data from RAM to FRAM
 */

void message_handler_application_save_parameters()
{
	*((unsigned int *)&AppAdminStateSaved) = AppAdminState;
	*((unsigned int *)&AppHwOcSaved) = AppHwOc;
	*((unsigned int *)&AppDeadTimeSaved) = AppDeadTime;
	*((unsigned int *)&AppIfilterMaxSaved) = AppIfilterMax;
	*((unsigned int *)&AppIfilterMidSaved) = AppIfilterMid;
	*((unsigned int *)&AppImaxNonCmplSaved) = AppImaxNonCmpl;
	*((unsigned int *)&AppImaxNonCmplHysSaved) = AppImaxNonCmplHys;
	*((unsigned int *)&AppThrottleMinVoltageSaved) = AppThrottleMinVoltage;
	*((unsigned int *)&AppPwmLowUpperBoundSaved) = AppPwmLowUpperBound;
	*((unsigned int *)&AppPwmMidUpperBoundSaved) = AppPwmMidUpperBound;
	*((unsigned int *)&AppPwmMinDutySaved) = AppPwmMinDuty;
	*((unsigned int *)&AppOffThrottleSaved) = AppOffThrottle;
	
	afe_data_save_parameters();
	
	// Reset device after FRAM changes
//	drv922xxReset();
}


/*
 *  \fn void message_handler_application_reset_defaults()
 *  \brief Callback function from common message handler for resetting application data in FRAM to factory defaults
 */

void message_handler_application_reset_defaults()
{
	
	*((unsigned int *)&AppAdminStateSaved) = DEFAULT_ADMIN_STATE;
	*((unsigned int *)&AppHwOcSaved) = DEFAULT_HW_OC;
	*((unsigned int *)&AppDeadTimeSaved) = DEFAULT_DEAD_TIME;
	*((unsigned int *)&AppIfilterMaxSaved) = DEFAULT_IFILT_MAX;
	*((unsigned int *)&AppIfilterMidSaved) = DEFAULT_IFILT_MID;
	*((unsigned int *)&AppImaxNonCmplSaved) = DEFAULT_I_MAX_NONCMPL_PWM;
	*((unsigned int *)&AppImaxNonCmplHysSaved) = DEFAULT_I_NONCMPL_PWM_HYS;
	*((unsigned int *)&AppThrottleMinVoltageSaved) = DEFAULT_THROTTLE_MIN_V;
	*((unsigned int *)&AppPwmLowUpperBoundSaved) = DEFAULT_PWM_LOW_UPPER_BOUND;
	*((unsigned int *)&AppPwmMidUpperBoundSaved) = DEFAULT_PWM_MID_UPPER_BOUND;
	*((unsigned int *)&AppPwmMinDutySaved) = DEFAULT_PWM_MIN_DUTY;
	*((unsigned int *)&AppOffThrottleSaved) = DEFAULT_OFF_THROTTLE;
	

	
	afe_data_reset_defaults();
	
	message_protocol_init_app();		// Copy parameters from FRAM to RAM
	
	// Reset device after FRAM changes
//	drv922xxReset();

}

